import ConstStrings from "./datas/strings";
import ScreenPositionUtil from "./ScreenPositionUtil";
import GameConfig from "./GameConfig";

var tabZoneMarginTopRatio = 0.052;
var tabZoneWID = 0; // tab区域宽度
var tabZoneHEI = 0; // tab区域高度
var tabZoneWHRatio = 6.0;
var tabItemWID = 0;
var tabItemHEI = 0;
var tabZoneStartX = 0; // 展示区起始位置
var tabZoneStartY = 0; // 展示区起始位置

var tabZoneMarginLeftRight = 0;// tab区域左右间隔  marginLeft marginRight

export default class SelectPartTab extends Laya.Sprite {
    constructor(lastViewEndPos) {
        super();
        // let lastViewEndPos = lastViewEndPos;
        this.sceneParent = null;

        tabZoneStartX = 0 + tabZoneMarginLeftRight; // 展示区起始位置
        tabZoneStartY = lastViewEndPos.y + ScreenPositionUtil.getPostionInStage(tabZoneMarginTopRatio, "y"); // 展示区起始位置

        tabZoneWID = Laya.stage.width - tabZoneMarginLeftRight * 2;
        tabZoneHEI = tabZoneWID / tabZoneWHRatio;

        tabItemWID = tabZoneWID / 4;
        tabItemHEI = tabZoneHEI;

        this.pos(tabZoneStartX, tabZoneStartY);
        this.graphics.drawRect(0, 0, tabZoneWID, tabZoneHEI, "#ffffff");

        this.viewTabItemList = new Laya.List;
        this.addChild(this.viewTabItemList);
        this.viewTabItemList.itemRender = SelectPartTabItem;
        this.viewTabItemList.repeatX = 5;
        this.viewTabItemList.repeatY = 1;

        this.viewTabItemList.mouseHandler = new Laya.Handler(this, this.onTabItemSelect);
        this.viewTabItemList.renderHandler = new Laya.Handler(this, this.updateTabItemItem);

        this.tabLineLay = new Laya.Sprite;
        this.addChild(this.tabLineLay);
        this.tabLineLay.pos(0, tabItemHEI * 0.97);
        this.tabLineLay.graphics.drawLine(0, 0, tabZoneWID, 0, "#cccccc", 2);

        let tabSelectRatio = 0.4;
        this.tabLineRect = new Laya.Sprite;
        this.tabLineLay.addChild(this.tabLineRect);
        this.tabLineRect.graphics.drawLine(0 + (tabSelectRatio - 0.05) * tabItemWID, -2, tabItemWID - tabSelectRatio * tabItemWID, -2, "#000000", 5);
    }

    getTabZoneEndPositionInStage() {
        return {
            x: tabZoneStartX + tabZoneWID,
            y: tabZoneStartY + tabZoneHEI,
        }
    }

    setLayoutData(inData) {
        inData[0].isSelect = true;
        this.moveFocusBySelect(0);
        this.dataLayout = inData;
        this.viewTabItemList.array = inData;
        if (this.sceneParent != null)
            this.sceneParent.updateTabSelect(0, this.viewTabItemList.array[0].type);

        this.viewTabItemList.selectedIndex = 0;
    }

    moveFocusBySelect(index) {
        Laya.Tween.to(this.tabLineRect, { x: index * tabItemWID }, 300, Laya.Ease.quartOut);
    }

    /**
 * list  鼠标选中
 * @param {*} e 
 * @param {*} index 
 */
    onTabItemSelect(e, index) {
        if (GameConfig.isUnableTouch)
            return;

        if (e.type != "mousedown")
            return;

        for (let i = 0; i < this.dataLayout.length; i++) {
            let tempCell = this.dataLayout[i];

            if (i != index)
                tempCell.isSelect = false;
            else {
                tempCell.isSelect = true;
            }
        }

        this.moveFocusBySelect(index);
        this.viewTabItemList.array = this.dataLayout;
        if (this.sceneParent != null)
            this.sceneParent.updateTabSelect(index, this.viewTabItemList.array[index].type);
        this.viewTabItemList.selectedIndex = index;
    }

    /**
     * list  ui更新
     * @param {*} e 
     * @param {*} index 
     */
    updateTabItemItem(cell, index) {
        // cell.setTextStr(cell.dataSource.testStr);
        // cell.setTextStr(cell.dataSource.iconText);
        // cell.setSrcImg(cell.dataSource.iconUnselect);
        // cell.setSelectSrcImg(cell.dataSource.iconSelect);

        // cell.setFixState(cell.dataSource.isFix);
        cell.setSelectAndFix();
    }

    setSceneParent(e) {
        this.sceneParent = e;
    }

    updateFixState(inType, isFix) {
        for (let i = 0; i < this.viewTabItemList.length; i++) {
            let tempData = this.dataLayout[i];
            if (tempData.type == inType) {
                tempData.isFix = isFix;
                tempData.needFixAni = true;
                this.viewTabItemList.array = this.dataLayout;
                break;
            }
        }

        // debugger
        if (isFix) {


            let _self = this;
            let changeTabsAction = function (params) {

                let currentIndex = _self.viewTabItemList.selectedIndex;
                if (currentIndex + 1 >= _self.viewTabItemList.length)
                    return;
    
                let tempIndex = currentIndex + 1;
    
                for (let i = 0; i < _self.dataLayout.length; i++) {
                    let tempCell = _self.dataLayout[i];
    
                    if (i != tempIndex)
                        tempCell.isSelect = false;
                    else {
                        tempCell.isSelect = true;
                    }
                }



                _self.moveFocusBySelect(tempIndex);
                _self.viewTabItemList.array = _self.dataLayout;
                if (_self.sceneParent != null)
                    _self.sceneParent.updateTabSelect(tempIndex, _self.viewTabItemList.array[tempIndex].type);
                _self.viewTabItemList.selectedIndex = tempIndex;
            }

            Laya.timer.once(300, this, () => {
                changeTabsAction();
            }, null)
        }

    }
}

class SelectPartTabItem extends Laya.Box {
    constructor() {
        super();
        this.textWHRatio = 1.5;
        this.size(tabItemWID, tabItemHEI);
        // this.bgColor = "#ff000055",
        // this.pivot(this.width / 2, this.height / 2);

        this.textLay = new Laya.Sprite;
        this.addChild(this.textLay);
        this.textLay.pos(tabItemWID / 2, tabItemHEI / 2);
        // this.textLay.graphics.drawRect(0,0,5,"#000000");

        this.viewText = new Laya.Sprite;
        this.textLay.addChild(this.viewText);
        this.viewText.size(tabItemWID * 0.95, tabItemWID * 0.95 / this.textWHRatio);
        this.viewText.pivot(this.viewText.width * 0.5, this.viewText.height * 0.5);


        this.viewTextSign = new Laya.Sprite;
        this.textLay.addChild(this.viewTextSign);
        this.viewTextSign.size(this.viewText.height * 0.3, this.viewText.height * 0.3);
        this.viewTextSign.loadImage(ConstStrings.tabSelectSignImg);
        this.viewTextSign.pivot(this.viewTextSign.width * 0.5, this.viewTextSign.height * 0.5);
        this.viewTextSign.pos(this.viewText.width * 0.35, this.viewTextSign.height * -0.05);
        this.viewTextSign.alpha = 0;

        // this.tabLineLay  = new Laya.Sprite;
        // this.addChild(this.tabLineLay);
        // this.tabLineLay.pos(0, tabItemWID * 0.75);
        // this.tabLineLay.graphics.drawLine(0, 0, tabItemWID, 0, "#cccccc", 2);

        // this.tabLineRect  = new Laya.Sprite;
        // this.tabLineLay.addChild(this.tabLineRect);
        // // this.tabLineRect.pos(0, tabItemWID * 0.9);
        // this.tabLineRect.graphics.drawLine(0 + 40, 0, tabItemWID - 40, 0, "#000000", 5);
        // this.tabLineRect.alpha = 0;

    }

    setSrcImg(e) {
        // this.iconImage.loadImage(e);
    }

    setSelectSrcImg(e) {
        // this.iconSelectImage.loadImage(e);
    }

    setTextStr(e) {
        // this.viewText.text = e;
        this.viewText.loadImage(e)
    }

    setSelectState(e) {
        // if (e) {
        //     Laya.Tween.to(this.viewText, { scaleX: 1.2, scaleY: 1.2 }, 800, Laya.Ease.quartOut); // 字
        //     this.viewText.alpha = 1.;
        // } else {
        //     Laya.Tween.to(this.viewText, { scaleX: 1.0, scaleY: 1.0 }, 800, Laya.Ease.quartOut);
        //     this.viewText.alpha = 0.5;
        // }
        let isS = this.dataSource.isSelect;
        let isF = this.dataSource.isFix;

        if (isS) {
            Laya.Tween.to(this.viewText, { scaleX: 1.2, scaleY: 1.2 }, 800, Laya.Ease.quartOut); // 字
            this.viewText.alpha = 1.;
        } else if (!isS && isF) {
            Laya.Tween.to(this.viewText, { scaleX: 1.2, scaleY: 1.2 }, 800, Laya.Ease.quartOut); // 字
            this.viewText.alpha = 1.;
        } else if (!isS && !isF) {
            Laya.Tween.to(this.viewText, { scaleX: 1.0, scaleY: 1.0 }, 800, Laya.Ease.quartOut);
            this.viewText.alpha = 0.5;
        }
    }

    setFixState(e) {
        if (!this.dataSource.needFixAni) {
            // if (e) {
            //     this.viewTextSign.loadImage(ConstStrings.tabSelectSignImg);
            //     this.viewTextSign.alpha = 1;
            //     Laya.Tween.to(this.viewTextSign, { alpha: 1 }, 400, Laya.Ease.quartOut);
            // } else {
            //     this.viewTextSign.alpha = 0;
            // }
            return;
        }


        this.dataSource.needFixAni = false;
        if (!e) {
            this.viewTextSign.loadImage(ConstStrings.tabWrongSelectSignImg);
            Laya.Tween.to(this.viewTextSign, { alpha: 1 }, 300, Laya.Ease.quartOut);
            Laya.Tween.to(this.viewTextSign, { alpha: 0 }, 300, Laya.Ease.quartOut, null, 1700);
        }

        this.setSelectAndFix()
    }

    setSelectAndFix() {
        let isS = this.dataSource.isSelect;
        let isF = this.dataSource.isFix;

        if (!isS && !isF) {
            this.viewText.loadImage(this.dataSource.iconTextUnselect)
        } else if (!isS && isF)
            this.viewText.loadImage(this.dataSource.iconTextFix)
        else if (isS && !isF)
            this.viewText.loadImage(this.dataSource.iconText)
        else if (isS && isF)
            this.viewText.loadImage(this.dataSource.iconTextFix)



        if (!this.dataSource.needFixAni) {
            return;
        }


        this.dataSource.needFixAni = false;
        if (!isF) {
            this.viewTextSign.loadImage(ConstStrings.tabWrongSelectSignImg);
            Laya.Tween.to(this.viewTextSign, { alpha: 1 }, 300, Laya.Ease.quartOut);
            Laya.Tween.to(this.viewTextSign, { alpha: 0 }, 300, Laya.Ease.quartOut, null, 800);
        }


    }





}

